<?php
	require('follow.doc.php');
	//关注
	$app->post(
		'/follow/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['followuserid']) && isset($data['followusertype']) && isset($data['userid']) && isset($data['usertype'])){
				$follow = $db2->select(1, 'tb_follow', '*', array(
					'follow_userid' => $data['followuserid'],
					'follow_usertype' => $data['followusertype'],
					'userid' => $data['userid'],
					'usertype' => $data['usertype']
				));
				if(!$follow){
					$db2->insert('tb_follow', array(
						'follow_userid' => $data['followuserid'],
						'follow_usertype' => $data['followusertype'],
						'userid' => $data['userid'],
						'usertype' => $data['usertype'],
						'#dt' => 'now()'
					));
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//取消关注
	$app->post(
		'/follow/delete',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['followuserid']) && isset($data['followusertype']) && isset($data['userid']) && isset($data['usertype'])){
				$db2->delete('tb_follow', array(
					'follow_userid' => $data['followuserid'],
					'follow_usertype' => $data['followusertype'],
					'userid' => $data['userid'],
					'usertype' => $data['usertype'],
				));
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//关注列表
	$app->get(
		'/follow/list',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['userid']) && isset($data['usertype'])){
				$echo['data'] = array();
				$sqlwhere=array('userid' => $data['userid'], 'usertype' => $data['usertype']);
				if(isset($data['findusertype'])){
					$sqlwhere['follow_usertype'] = $data['findusertype'];
				}
				$echo['count'] = $db2->select(2, 'tb_follow', '*', $sqlwhere);
				$sqlwhere['limit'] = array($data['from'], $data['to']);
				$sqlwhere['order'] = 'dt desc';
				$follow = $db2->select(0, 'tb_follow', '*', $sqlwhere);
				if($follow){
					foreach($follow as $v){
						$tb = $v['follow_usertype'] == 1 ? 'tb_member' : 'tb_coach';
						$user = $db2->select(1, $tb, '*', array('tbid' => $v['follow_userid']));
						$tmp['userid'] = $v['follow_userid'];
						$tmp['usertype'] = $v['follow_usertype'];
						if($v['follow_usertype'] == 1){
							$tmp['head'] = file_exists('dofiles/member/'.$user['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$user['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						}else{
							$tmp['head'] = file_exists('dofiles/coach/'.$user['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$user['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						}
						$tmp['mobile'] = substr_replace($user['mobile'], '****', 3, 4);
						$tmp['nickname'] = $user['nickname'];
						$echo['data'][] = $tmp;
					}
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
